package com.seadragon.app.ksr.model;

import javax.persistence.Entity;
import javax.persistence.EntityResult;
import javax.persistence.FieldResult;
import javax.persistence.Id;
import javax.persistence.NamedNativeQueries;
import javax.persistence.NamedNativeQuery;
import javax.persistence.SqlResultSetMapping;
import javax.persistence.SqlResultSetMappings;

import org.hibernate.annotations.Type;

import com.vividsolutions.jts.geom.Point;

@Entity
@NamedNativeQueries({ 
	@NamedNativeQuery(name = "geocode", 
			query = "SELECT (addy).parsed, 1 As id, 6 as addressType, null As county, null As neighborhood, g.rating, g.geomout As pos, (addy).address As streetNumber, (addy).predirAbbrev As predir, (addy).streetname As streetName, (addy).streettypeabbrev As streetType, (addy).postdirAbbrev As postDir , (addy).internal As internal, (addy).location As city, (addy).stateabbrev As state,(addy).zip FROM geocode(:addressString) As g", 
			resultClass = GeocodedAddress.class),
	@NamedNativeQuery(name = "normalize", 
	query = "SELECT (addy).parsed, 1 As id, 6 as addressType, null As county, null As neighborhood, g.rating, g.geomout As pos, (addy).address As streetNumber, (addy).predirAbbrev As predir, (addy).streetname As streetName, (addy).streettypeabbrev As streetType, (addy).postdirAbbrev As postDir , (addy).internal As internal, (addy).location As city, (addy).stateabbrev As state,(addy).zip FROM geocode(:addressString) As g", 
	resultClass = GeocodedAddress.class) 
})
@SqlResultSetMappings({ 
	@SqlResultSetMapping(name = "geocodedAddressByFields", entities = { @EntityResult(entityClass = GeocodedAddress.class, fields = {
		@FieldResult(name = "rating", column = "rating"), @FieldResult(name = "lon", column = "lon"),
		@FieldResult(name = "lat", column = "lon"), @FieldResult(name = "streetNumber", column = "stno"),
		@FieldResult(name = "streetName", column = "street"), @FieldResult(name = "streetType", column = "styp"),
		@FieldResult(name = "city", column = "city"), @FieldResult(name = "state", column = "st"),
		@FieldResult(name = "zip", column = "zip") }) }) 
})
//@Spatial(name="location")
public class GeocodedAddress/* implements Coordinates*/{
	@Id
	private int id;
	private int rating;
	@Type(type = "org.hibernate.spatial.GeometryType") 
	private Point pos;
	private String preDir;
	private String streetNumber;
	private String streetName;
	private String streetType;
	private String postDir;
	private String internal;
	private String city;
	private String state;
	private String county;
	private String neighborhood;
	private String zip;
	private AddressType addressType;
	private boolean parsed;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getRating() {
		return rating;
	}

	public void setRating(int rating) {
		this.rating = rating;
	}

	public Point getPos() {
		return pos;
	}

	public void setPos(Point pos) {
		this.pos = pos;
	}

	public AddressType getAddressType() {
		return addressType;
	}

	public void setAddressType(AddressType addressType) {
		this.addressType = addressType;
	}

	public String getCounty() {
		return county;
	}

	public void setCounty(String county) {
		this.county = county;
	}

	public String getNeighborhood() {
		return neighborhood;
	}

	public void setNeighborhood(String neighborhood) {
		this.neighborhood = neighborhood;
	}

	public String getStreetNumber() {
		return streetNumber;
	}

	public void setStreetNumber(String streetNumber) {
		this.streetNumber = streetNumber;
	}

	public String getStreetName() {
		return streetName;
	}

	public void setStreetName(String streetName) {
		this.streetName = streetName;
	}

	public String getStreetType() {
		return streetType;
	}

	public void setStreetType(String streetType) {
		this.streetType = streetType;
	}

	public String getPreDir() {
		return preDir;
	}

	public void setPreDir(String preDir) {
		this.preDir = preDir;
	}

	public String getPostDir() {
		return postDir;
	}

	public void setPostDir(String postDir) {
		this.postDir = postDir;
	}

	public String getInternal() {
		return internal;
	}

	public void setInternal(String internal) {
		this.internal = internal;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public String getZip() {
		return zip;
	}

	public void setZip(String zip) {
		this.zip = zip;
	}

	public boolean isParsed() {
		return parsed;
	}

	public void setParsed(boolean parsed) {
		this.parsed = parsed;
	}

	public boolean isAddress(){
		return streetName != null;
	}
	
	public boolean isZip() {
		return zip != null;
	}
	
	public boolean isCity() {
		return city != null;
	}

//	@Override
	public Double getLatitude() {
		return pos.getY();
	}

//	@Override
	public Double getLongitude() {
		return pos.getX();
	}
}
